#!/usr/bin/env bash

source ./config/env.sh
clear


for node_ip in ${NODE_IP}
do
  echo "=========================================== ${node_ip} ==========================================="
  scp /etc/systemd/system/flanneld.service root@${node_ip}:/etc/systemd/system/
done

for node_ip in ${NODE_IP}
do
  echo "=========================================== ${node_ip} ==========================================="
  ssh root@${node_ip} "systemctl daemon-reload && systemctl enable flanneld && systemctl restart flanneld"
done

for node_ip in ${NODE_IP}
do
  echo "=========================================== ${node_ip} ==========================================="
  ssh root@${node_ip} "systemctl restart flanneld"
done

# 查看状态
for node_ip in ${NODE_IP}
do
  echo "=========================================== ${node_ip} ==========================================="
  ssh root@${node_ip} "sudo systemctl status flanneld | grep Active"
done


# 查看集群 Pod 网段(/16）
sleep 5
${K8S_BIN_DIR}/etcdctl \
  --endpoints=${ETCD_NODES} \
  --ca-file=${K8S_CA_DIR}/admin/ca.pem \
  --cert-file=${K8S_CA_DIR}/flannel/flanneld.pem \
  --key-file=${K8S_CA_DIR}/flannel/flanneld-key.pem \
  get ${FLANNEL_ETCD_PREFIX}/config >> ${K8S_LOGS_DIR}/flanneld-cidr-msg.log

# 查看已分配的 Pod 子网段列表(/24)
sleep 5
${K8S_BIN_DIR}/etcdctl \
  --endpoints=${ETCD_NODES} \
  --ca-file=${K8S_CA_DIR}/admin/ca.pem \
  --cert-file=${K8S_CA_DIR}/flannel/flanneld.pem \
  --key-file=${K8S_CA_DIR}/flannel/flanneld-key.pem \
  ls ${FLANNEL_ETCD_PREFIX}/subnets >> ${K8S_LOGS_DIR}/flanneld-cidr-msg.log

# 验证各节点能通过 Pod 网段互通
for node_ip in ${NODE_IP}
do
  echo "=========================================== ${node_ip} ==========================================="
  ssh root@${node_ip} "/usr/sbin/ip addr show flannel.1|grep -w inet"
done

if [ $? == 0 ]
then
  echo "write flanneld in etcd succeed ${K8S_LOGS_DIR}/flanneld-cidr-msg.log"
else
  echo -e "\033[31m write flanneld in etcd succeed failed\033[0m" 
  exit
fi




# 查看某一 Pod 网段对应的节点 IP 和 flannel 接口地址:
#etcdctl \
#  --endpoints=${ETCD_ENDPOINTS} \
#  --ca-file=/etc/kubernetes/cert/ca.pem \
#  --cert-file=/etc/flanneld/cert/flanneld.pem \
#  --key-file=/etc/flanneld/cert/flanneld-key.pem \
#  get ${FLANNEL_ETCD_PREFIX}/subnets/172.30.96.0-21